package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.*;

/**
 * @author Ann Raboschuk
 */
public class View extends JPanel
{
   private static final long serialVersionUID = 1L;
   private JTable table;
   private Controller controller;
   
   public View(Controller controller)
   {
      this.controller = controller;
      this.setLayout(new BorderLayout());
      CustomTableModel model = new CustomTableModel(controller.getPeople());
      table = new JTable(model);
      View.packColumns(table);
      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);      
      add(new JScrollPane(table), BorderLayout.CENTER);
      add(createControls(), BorderLayout.SOUTH);
   }
   
   private Component createControls()
   {
      Box controls = Box.createHorizontalBox();
      String[] buttons = {"Add...", "Edit...", "Delete"};
      ActionListener btnListener = new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent event)
         {
            switch (event.getActionCommand().charAt(0))
            {
            case 'A'://add
               controller.addPerson(new PersonDialog().showDialog());
               break;
            case 'E'://edit
               controller.editPerson(table.getSelectedRow(), new PersonDialog(getSelectedPerson()).showDialog());
               break;
            case 'D'://delete
               int selection = JOptionPane.showConfirmDialog(table, "Delete selected person?");
               if (selection == JOptionPane.YES_NO_OPTION)
                  controller.deletePerson(table.getSelectedRow());
            }
            redraw();
         }
      };
      for (int i=0; i<buttons.length; i++)
      {
         controls.add(Box.createHorizontalStrut(MVC.getDefaultWidth()/6));
         JButton button = new JButton(buttons[i]);
         button.setActionCommand(buttons[i]);
         button.addActionListener(btnListener);
         controls.add(button);
      }
      return controls;
   }
   
   public void redraw()
   {
      table.revalidate();
      table.repaint();
      packColumns(table);
   }
   
   public static void packColumns(JTable table)
   {
      for (int i = 0; i < table.getColumnCount(); i++)
      {
         TableCellRenderer renderer = table.getDefaultRenderer(table.getModel().getColumnClass(i));
         int maxWidth = 0;
         for (int j = 0; j < table.getRowCount(); j++)
         {
            Component component = renderer.getTableCellRendererComponent(table, table.getValueAt(j, i), true, true, j, i);
            maxWidth = Math.max(maxWidth, component.getPreferredSize().width);
         }
         TableColumn column = table.getColumnModel().getColumn(i);
         column.setMinWidth(maxWidth + table.getIntercellSpacing().width);
      }
   }
   
   public String[] getSelectedPerson()
   {
      String[] person = null;
      int row = table.getSelectedRow();
      if (row != -1)
      {
         int endIndex = table.getColumnCount();
         person = new String[endIndex];
         for (int i = 0; i < endIndex; i++)
            person[i] = table.getValueAt(row, i).toString();
      }
      return person;
   }
}
